classdef MainController < handle
    properties
        gui
        nmrg
        
        currentJob
    end
    
    methods
        function obj = MainController(nmrg)
            obj.nmrg = nmrg;
            obj.makeGUI();
        end
        
        %%
        function obj = makeGUI(obj)
            obj.gui = Miscellaneous.GUICreator.GUICreator('Name', 'NMRG Main Controller');

            % Global Grid
            grd = obj.gui.make_grid([], [1, 2], 'ColumnWidth', {200, '5x'});
            
            % subgrid 1: Panel: Data Select
            subgrd1 = grd.subGrid_in_Panel(1, 1, [6 1], 'Measurement Jobs', ...
                                          'RowHeight', {'1x', 20, 20, 20, 20, '1x'}, 'Padding', [30 0 30 0]);
            hdls.botton1  = subgrd1.placeUI('uibutton', 2, 1, 'Text', 'GetRFSpectrum', 'ButtonPushedFcn', @(btn,event) obj.openJobParamWin('GetRFSpectrum') );
            hdls.botton2  = subgrd1.placeUI('uibutton', 3, 1, 'Text', 'GetWorkingPoint', 'ButtonPushedFcn', @(btn,event) obj.openJobParamWin('GetWorkingPoint') );
            
            % Panel : Plot
            hdls.panelPlot = grd.placeUI('uipanel', 1, 2, 'Title', 'Plots', 'AutoResizeChildren', 'off');
            
            obj.gui.uiHandle = hdls;
        end
        
        %%
        function show(obj)
            if ~isvalid(obj.gui.figureHandle)
                obj.makeGUI().show();
            end
            
            if ~obj.gui.isShown()
                obj.gui.show();
            end
        end
        
        function hide(obj)
            obj.gui.hide();
        end
        
        %%
        function jobRun(obj, param_table, param_window)
            obj.currentJob.run(); 
            disp(param_table.Data); pause(1);
            delete(param_window);
        end
        function obj = openJobParamWin(obj, jobname)
            switch jobname
                case 'GetRFSpectrum'
                    obj.currentJob = Table1.NMRGJobs.GetRFSpectrum(obj.nmrg);
                case 'GetWorkingPoint'
                    obj.currentJob = Table1.NMRGJobs.GetWorkingPoint(obj.nmrg);
            end
            
            obj.currentJob.parseParameters(); 
            param = obj.currentJob.dataPackage.parameters;
            
            param_window = Miscellaneous.GUICreator.GUICreator('Name', [jobname ' Parameters'], 'Unit', 'Absolute', 'Width', 500, 'Height', 800);
            param_grd = param_window.make_grid([], [2, 3], 'RowHeight',   {'1x', 20}, 'ColumnWidth', {'1x', '1x', '1x'});
            param_table = param_grd.placeUI('uitable',  1, [1, 3]);
            run_button  = param_grd.placeUI('uibutton', 2, 2, 'Text', 'Run', 'ButtonPushedFcn', @(btn,event) obj.jobRun(param_table, param_window) ); %#ok<NASGU>

            param_table.Visible = 'off';
            param_table.Data = struct2cell( param );
            param_table.RowName = fieldnames( param );
            param_table.ColumnName = {'Values'};
            param_table.ColumnEditable = true;
            s = uistyle; s.HorizontalAlignment = 'center';
            param_table.addStyle(s);
            param_table.Visible = 'on';
            param_window.show();
        end

    end
end

